package com.wannengbang.cloudleader.homepage.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.DeviceUnifyListBean;
import com.wannengbang.cloudleader.homepage.MachinesInfoActivity;
import com.wannengbang.cloudleader.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 合伙人列表
 */
public class MachinesListAdapter extends RecyclerView.Adapter<MachinesListAdapter.ViewHolder> {

    private List<DeviceUnifyListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private String status;
    private String tag;

    public MachinesListAdapter(List<DeviceUnifyListBean.DataBean.ItemListBean> listsBeans, String status, String tag) {
        this.listsBeans = listsBeans;
        this.status = status;
        this.tag = tag;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_machines_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DeviceUnifyListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        if (TextUtils.isEmpty(dataBean.getEnable_up_date())) {
            holder.tvEnableUpDate.setVisibility(View.GONE);
        } else {
            holder.tvEnableUpDate.setVisibility(View.VISIBLE);
        }
        holder.tvEnableUpDate.setText("激活截止日期至：" + dataBean.getEnable_up_date());
        holder.tvNumber.setText(dataBean.getNo());
        if (dataBean.getRate_type() == 4) {
            holder.tvRateType.setText("综合类");
        } else {
            holder.tvRateType.setText("高端类");
        }
        if (dataBean.isCheck()) {
            holder.ivCheck.setImageResource(R.mipmap.ic_order_xuanze);
        } else {
            holder.ivCheck.setImageResource(R.mipmap.ic_order_kongyuan);
        }
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, MachinesInfoActivity.class);
            intent.putExtra("id", dataBean.getId() + "");
            context.startActivity(intent);
        });
        if (dataBean.getType() == 3) {
            holder.tvRateVip.setVisibility(View.VISIBLE);
        } else {
            holder.tvRateVip.setVisibility(View.GONE);
        }
        if (dataBean.getType() == 4) {
            holder.tvRatePlus.setVisibility(View.VISIBLE);
        } else {
            holder.tvRatePlus.setVisibility(View.GONE);
        }
        if (dataBean.getIs_send() == 0) {
            if (dataBean.getType() == 1) {
                holder.tvType.setText("采购的云小宝");
            } else if (dataBean.getType() == 2) {
                holder.tvType.setText("采购的云商宝");
            } else if (dataBean.getType() == 3) {
                holder.tvType.setText("采购的云小宝");
            } else if (dataBean.getType() == 4) {
                holder.tvType.setText("采购的云小宝");
            }
        } else {
            holder.tvType.setText("兑换的云小宝");
        }


        //0 未使用  1  已激活  2 已禁用  3已划拨
        if ("0".equals(status) || "1".equals(status) || "3".equals(status)) {
            if (TextUtils.isEmpty(tag)) {
                holder.ivCheck.setVisibility(View.GONE);
            } else {
                holder.ivCheck.setVisibility(View.VISIBLE);
            }
        } else {
            holder.ivCheck.setVisibility(View.GONE);
        }
        holder.llCheck.setOnClickListener(v -> {
            if ("0".equals(status) || "1".equals(status) || "3".equals(status)) {
                if (TextUtils.isEmpty(tag)) {
                    Intent intent = new Intent(context, MachinesInfoActivity.class);
                    intent.putExtra("id", dataBean.getId() + "");
                    context.startActivity(intent);
                } else {
                    dataBean.setCheck(!dataBean.isCheck());
                    notifyDataSetChanged();
                }
            } else {
                Intent intent = new Intent(context, MachinesInfoActivity.class);
                intent.putExtra("id", dataBean.getId() + "");
                context.startActivity(intent);
            }
        });
        holder.tvNumber.setOnClickListener(v -> {
            Intent intent = new Intent(context, MachinesInfoActivity.class);
            intent.putExtra("id", dataBean.getId() + "");
            context.startActivity(intent);
        });
        holder.tvNumber.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //获取剪贴板管理器
                ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", dataBean.getNo());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                return true;
            }
        });
        holder.tvCopy.setOnClickListener(v -> {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", dataBean.getNo());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.showShort("复制成功");
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_check)
        ImageView ivCheck;
        @BindView(R.id.ll_check)
        LinearLayout llCheck;
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_copy)
        TextView tvCopy;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_rate_type)
        TextView tvRateType;
        @BindView(R.id.tv_enable_up_date)
        TextView tvEnableUpDate;
        @BindView(R.id.tv_rate_vip)
        TextView tvRateVip;
        @BindView(R.id.tv_rate_plus)
        TextView tvRatePlus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
